package com.csht.manager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.csht.dao.CompanyDao;
import com.csht.entity.CompanyEntity;
import com.csht.manager.CompanyManager;
import com.csht.vo.CompanyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.winit.common.orm.mybatis.PageBase;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.pms.spi.v2.common.PageVo;
import com.winit.pms.utils.SearchableUtil;
import com.winit.common.query.Sort.Direction;

/**
 * 运输企业managerImpl实现类
 * 
 * @version
 * 
 * <pre>
 * Author	Version		Date		Changes
 * houzw    1.0  2018年01月05日 Created
 * </pre>
 * 
 * @since 1.
 */
@Component("companyManager")
public class CompanyManagerImpl implements CompanyManager {

    private Logger    logger = LoggerFactory.getLogger(CompanyManagerImpl.class);

    @Resource
    private CompanyDao companyDao;

    @Override
    public CompanyVo getCompany(Long id) {
        logger.info("单个查询：{}", id);
        CompanyEntity entity = companyDao.get(id);
        CompanyVo newVo = null;
        if (entity != null) {
            newVo = new CompanyVo();
            this.copyEntityToVo(entity, newVo);
        }
        return newVo;
    }

    @Override
    public Long createCompany(CompanyVo vo) {
        logger.info("新增：{}", vo);
        CompanyEntity entity = new CompanyEntity();
        this.copyVoToEntity(vo, entity);

        // TODO: 是否需要校验已存在
        companyDao.add(entity);
        return entity.getId();
    }

    @Override
    public void createBatchCompany(List<CompanyVo> vos) {
        logger.info("批量新增：{}", vos);
        List<CompanyEntity> list = new ArrayList<CompanyEntity>();
        for (CompanyVo vo : vos) {
            CompanyEntity entity = new CompanyEntity();
            this.copyVoToEntity(vo, entity);
            list.add(entity);
        }
        companyDao.addBatch(list);
    }

    @Override
    public void deleteCompany(Long id) {
        logger.info("删除：{}", id);
        companyDao.delete(id);
    }

    @Override
    public void deleteBatchCompany(List<Long> ids) {
        logger.info("批量删除：{}", ids);
        companyDao.deleteBatch(ids);
    }

    @Override
    public void updateCompany(CompanyVo vo) {
        logger.info("更新：{}", vo);
        CompanyEntity entity = new CompanyEntity();
        this.copyVoToEntity(vo, entity);
        return companyDao.updateSingle(entity);
    }

    @Override
    public long updateBatchCompany(List<CompanyVo> vos) {
        logger.info("批量更新：{}", vos);
        List<CompanyEntity> list = new ArrayList<CompanyEntity>();
        for (CompanyVo vo : vos) {
            CompanyEntity entity = new CompanyEntity();
            this.copyVoToEntity(vo, entity);
            list.add(entity);
        }
        companyDao.updateBatch(list);
    }

    @Override
    public Page<CompanyVo> findCompanyPage(PageVo pageVo, CompanyVo vo) {
        logger.info("分页查询：{}, vo:{}", pageVo, vo);
        Searchable<CompanyEntity> searchable = this.buildSearchable(pageVo, vo);
        PageBase<CompanyEntity> page = companyDao.findPage(searchable);
        List<CompanyVo> vos = new ArrayList<CompanyVo>();
        for (CompanyEntity entity : page) {
            CompanyVo newVo = new CompanyVo();
            this.copyEntityToVo(entity, newVo);
            vos.add(newVo);
        }
        return new Page<CompanyVo>(vos, page.getPageable(), page.getTotalElements());
    }

    @Override
    public List<CompanyVo> queryCompanyList(CompanyVo vo) {
        logger.info("查询所有：{}", vo);
        CompanyEntity entity = new CompanyEntity();
        this.copyVoToEntity(vo, entity);
        List<CompanyEntity> list = companyDao.findList(entity);;
        List<CompanyVo> listVo = new ArrayList<CompanyVo>();
        for (CompanyEntity newEntity : list) {
            CompanyVo newVo = new CompanyVo();
            this.copyEntityToVo(newEntity, newVo);
            listVo.add(newVo);
        }
        return listVo;
    }

    @SuppressWarnings("unchecked")
    private Searchable<CompanyEntity> buildSearchable(PageVo pageVo, CompanyVo vo) {
        Searchable<CompanyEntity> searchable = SearchableUtil.getSearchable(pageVo);
        
        if (searchable.getSort() == null) {
            searchable.addSort(Direction.DESC, "CREATED");
        }
        
        if (vo == null) {
            return searchable;
        }
        
        // TODO:添加条件 searchable.addSearchFilter("USERNAME", SearchOperator.like,
        // "%" + vo.getUsername() + "%");
        
        return searchable;
    }
    
    /**
     * 将实体属性拷贝到vo
     */
    private void copyEntityToVo(CompanyEntity entity, CompanyVo vo) {
        if (entity == null) return;
        vo.setId(entity.getId());
        vo.setLicenceno(entity.getLicenceno());
        vo.setCompName(entity.getCompName());
        vo.setEconomy(entity.getEconomy());
        vo.setIssuedate(entity.getIssuedate());
        vo.setEnddate(entity.getEnddate());
        vo.setManager(entity.getManager());
        vo.setPhone(entity.getPhone());
        vo.setAddress(entity.getAddress());
        vo.setBusiness(entity.getBusiness());
        vo.setParentid(entity.getParentid());
        vo.setVehicleSum(entity.getVehicleSum());
        vo.setEmployeeSum(entity.getEmployeeSum());
        vo.setCompType(entity.getCompType());
        vo.setLastUser(entity.getLastUser());
        vo.setLastTime(entity.getLastTime());
    }
    
    /**
     * 将vo属性拷贝到实体
     */
    private void copyVoToEntity(CompanyVo vo, CompanyEntity entity) {
        if (vo == null) return;
        entity.setId(vo.getId());
        entity.setLicenceno(vo.getLicenceno());
        entity.setCompName(vo.getCompName());
        entity.setEconomy(vo.getEconomy());
        entity.setIssuedate(vo.getIssuedate());
        entity.setEnddate(vo.getEnddate());
        entity.setManager(vo.getManager());
        entity.setPhone(vo.getPhone());
        entity.setAddress(vo.getAddress());
        entity.setBusiness(vo.getBusiness());
        entity.setParentid(vo.getParentid());
        entity.setVehicleSum(vo.getVehicleSum());
        entity.setEmployeeSum(vo.getEmployeeSum());
        entity.setCompType(vo.getCompType());
        entity.setLastUser(vo.getLastUser());
        entity.setLastTime(vo.getLastTime());
    }
}
